@khoohaoyit/image-size
Version:
A stream based image size extractor in Node
41 lines (40 loc) • 2.33 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractCurSize = exports.extractIcoSize = void 0;
const TYPE_ICON = 1;
const TYPE_CURSOR = 2;
const SIZE_HEADER = 2 + 2 + 2;
const SIZE_IMAGE_ENTRY = 1 + 1 + 1 + 1 + 2 + 2 + 4 + 4;
const extractSize = (targetImageType) => {
return function (stream) {
return __asyncGenerator(this, arguments, function* () {
const reserved = yield __await(stream.readUInt16LE(0));
const imageCount = yield __await(stream.readUInt16LE(4));
if (reserved !== 0 || imageCount === 0)
return yield __await(void 0);
const imageType = yield __await(stream.readUInt16LE(2));
if (imageType !== targetImageType)
return yield __await(void 0);
for (let amount = imageCount, offset = SIZE_HEADER; amount; --amount, offset += SIZE_IMAGE_ENTRY) {
yield yield __await({
width: (yield __await(stream.readUint8(offset))) || 256,
height: (yield __await(stream.readUint8(offset + 1))) || 256,
});
}
});
};
};
exports.extractIcoSize = extractSize(TYPE_ICON);
exports.extractCurSize = extractSize(TYPE_CURSOR);